package com.d404.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import im.beans.Group;
import im.beans.User;
import im.notify.server.Notify;
import manager.MessageManager;
import manager.PersistentManager;

/**
 * 从群组中移除账号
 * Servlet implementation class DeleteUserFromGroup
 */
public class DeleteUserFromGroup extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteUserFromGroup() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			// 删除群组信息
			Query q = session.createQuery("delete from Group_User gu where gu.group = ? and gu.user = ?");
			Group gTmep = new Group();
			gTmep.setId(Long.parseLong(upload.getGroupID()));
			q.setParameter(0, gTmep);
			User tUser = new User();
			tUser.setId(Long.parseLong(upload.getUserID()));
			q.setParameter(1, tUser);
			q.executeUpdate();
			
			tx.commit();
			
//			Notify.BeRemovedFromGroup beRemovedFromGroup = new Notify.BeRemovedFromGroup();
//			beRemovedFromGroup.setGroupId(upload.getGroupID());
//			MessageManager.broadcast(JSON.toJSONString(beRemovedFromGroup));
			download.setStatus(Download.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(Download.FAILED);
			tx.rollback();
			e.printStackTrace();
		}finally {
			response.getWriter().append(JSON.toJSONString(download));
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	// 上行数据
	public static class Upload
	{
		String groupID;
		String userID;
		public String getGroupID() {
			return groupID;
		}
		public void setGroupID(String groupID) {
			this.groupID = groupID;
		}
		public String getUserID() {
			return userID;
		}
		public void setUserID(String userID) {
			this.userID = userID;
		}
		
	}

	// 下行数据
	public static class Download
	{
		public static final int SUCCESS = 1;
		public static final int FAILED = 2;
		int status;
		public int getStatus() {
			return status;
		}
		public void setStatus(int status) {
			this.status = status;
		}
		
	}
}
